﻿#s2-生成词云
from wordcloud import WordCloud
import matplotlib.pyplot as plt #绘制图像的模块
import jieba #jieba分词
path_txt='xxxxxxxxxxxxxxxx.txt'
f = open(path_txt,'r',encoding='UTF-8').read()

# 结巴分词，生成字符串，wordcloud无法直接生成正确的中文词云
stopwords = [line.strip() for line in open("xxxxxxxxxxxxxxxx.txt",encoding="utf-8").readlines()]  
words  = jieba.lcut(f)  
cut_text = " ".join(words)
wordcloud = WordCloud(
    #设置字体，不然会出现口字乱码，文字的路径是电脑的字体一般路径，可以换成别的
    font_path="C:/Windows/Fonts/msyh.ttc",
    #设置了背景，宽高
    #background_color="white",
    width=1000,height=880,
    collocations=False,
    font_step=10,
    max_words=70
    ).generate(cut_text)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
